package com.focus.pay.core.utils;

import java.util.Iterator;
import java.util.Locale;
import java.util.Objects;

/**
 * @author 赵参谋
 * @version $
 * =========================================================================
 * 变更履历：
 * -------------------------------------------------------------------------
 * 变更编号     变更时间    变更人   变更原因    变更内容
 * -------------------------------------------------------------------------
 * <p>
 * @description：
 * @date 2022/1/7 13:27
 **/
public class StringUtils {
    public static final String EMPTY    = "";
    public static final String ONE      = "1";
    public static final String TWO      = "2";
    public static final String THREE    = "3";

    private StringUtils() {
    }

    public static boolean isBlank(final String s) {
        if (s != null && !s.isEmpty()) {
            for(int i = 0; i < s.length(); ++i) {
                char c = s.charAt(i);
                if (!Character.isWhitespace(c)) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isEmpty(final CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotBlank(final String s) {
        return !isBlank(s);
    }

    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    public static String join(final Iterable<?> iterable, final char separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }

    public static String join(final Iterator<?> iterator, final char separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return Objects.toString(first, "");
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while(iterator.hasNext()) {
                    buf.append(separator);
                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String left(final String str, final int len) {
        if (str == null) {
            return null;
        } else if (len < 0) {
            return "";
        } else {
            return str.length() <= len ? str : str.substring(0, len);
        }
    }

    public static String quote(final String str) {
        return '\'' + str + '\'';
    }

    public static String trimToNull(final String str) {
        String ts = str == null ? null : str.trim();
        return isEmpty(ts) ? null : ts;
    }



    public static String toRootUpperCase(final String str) {
        return str.toUpperCase(Locale.ROOT);
    }

    public static String repeat(final String str, final int count) {
        Objects.requireNonNull(str, "str");
        if (count < 0) {
            throw new IllegalArgumentException("count");
        } else {
            StringBuilder sb = new StringBuilder(str.length() * count);

            for(int index = 0; index < count; ++index) {
                sb.append(str);
            }

            return sb.toString();
        }
    }
}
